

  <title>角色与菜单管理</title>

  <div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
      <a lay-href="">主页</a>
      <a><cite>权限中心</cite></a>
      <a><cite>角色与菜单管理</cite></a>
    </div>
  </div>
  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">角色与菜单管理</div>
          <div class="layui-card-body">
            <blockquote class="layui-elem-quote">点击下面表格中的角色，修改角色与菜单关系</blockquote>
            <table class="layui-hide" id="test-table-demoEvent" lay-filter="test-table-demoEvent"></table>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="site-text" style="margin: 5%; display: none" id="window1"  target="test1">
  	<form class="layui-form" method="post" >
		<div class="layui-card" >
			<div class="layui-card-header"></div>
			<div class="layui-card-body">
			
				<input type = "hidden"  id="roleId" name="roleId" lay-verify="roleId" autocomplete="off" placeholder="" class="layui-input">
			<div id="test-tree-demo1"></div>
			<div class="layui-btn-container">
				<div class="layui-form-item">
				  <div class="layui-input-block">
				<button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked" lay-filter="demo1">修改角色与菜单关系</button>
				</div>
				</div>
			</div>
			</div>
		</div>
  </form>
  </div>
  <script>
	  function data(){
	  	var result =[];
	  	$.get({
	  	  url: path + '/authority/Menu/selectMenuTree',
	  	  async:false,
	  	  success:function(res){
	  		  if(res.code == 0){
	  			  result = res.data;
	  		  }
	  	  }
	  	})
	  	return result;
	  }
  layui.use(['admin','tree','util', 'table'], function(){
    var table = layui.table
    ,tree = layui.tree
    ,layer = layui.layer
    ,util = layui.util
	
	
	
    table.render({
      elem: '#test-table-demoEvent'
      ,url: path + '/authority/Role/selectAll'
	  ,title: '角色数据表'
	  	,cols: [[
	  //    {type: 'checkbox', fixed: 'left'}
	  		{field:'roleId', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, hide:true}
	  		,{field:'roleNm', title:'角色名称', width:120}
	  		,{field:'roleDesc', title:'描述', width:300,event: 'setSign', style:'cursor: pointer;'}
	  		,{field:'schNm', title:'院校名称', width:120}
	  		,{field:'roleStatic', title:'角色状态', width:120, sort: true, templet: function(d){    //动态显示数据
	  			if(d.roleStatic == 1)
	  			{
	  				return '<span style="color:#0000FF;">' + '有效' + '</span>'
	  			}
	  			else
	  			{
	  				return '<span style="color:red;">' + '无效' + '</span>'
	  			}
	  		}}
	  		,{field:'addTime', title:'数据添加时间', width:150}
	  		,{field:'addUserId', title:'数据添加人', width:120}
	  		
	  	]]
	  	,request: {
	  		pageName: 'pageCurrent',  //当前页数
	  		limitName: 'pageSize'     //每页显示数据笔数
	  	}
	  	,limit: 5
	  	,limits: [5,10,15]
	  	,page: true
	  });
  
    table.on('tool(test-table-demoEvent)', function(obj){
      var data = obj.data;
	  $("#roleId").val(data.roleId);
      if(obj.event === 'setSign'){
		
		// selectmenu(data);
		
        layer.open({
        	title: '角色与菜单关系'
        	,area: ['600px', '300px']
        	,skin: 'layui-layer-molv'
        	,shadeClose: true
        	,type:1
        	,shade: 0 //遮罩透明度
        	,content: $("#window1")
			,done:tree.reload('test-tree-demoId1', {
          
			})
			,done:select(data)
        });
      }
    });
	function select(data){
		$.get({
			url: path + "/authority/RoleRel/selectmenuIdsByRoleId",
			data : 
			{
				roleId : $("#roleId").val()
			},
			success:function(res){
				if(res.code == 0){
					var id = res.data;
					tree.setChecked('test-tree-demoId1',id);
				}
				
			}
			
		});	
		
	}
	tree.render({
		
	  elem: '#test-tree-demo1'
	  ,onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
	  ,data: eval(data())
	  ,id: 'test-tree-demoId1'
	  ,showCheckbox: true
	  ,click: function(obj){
		console.log(obj.data)
	  }
	  
	});
	
	
	
	util.event('lay-demo', {
	  getChecked: function(obj){
	   var checkedData = tree.getChecked('test-tree-demoId1'); //获取选中节点的数据
	   var Ids = new Array();
	   var i = 0;
	   var j =0;
	   getCheck(checkedData);
	   var ids = Ids.toString();
	   function getCheck(checkedData){ 
		   for(i = 0;i< checkedData.length ;i++){
			    Ids.push(checkedData[i].id) ;
		   		if(checkedData[i].children != null){
					var children1 = checkedData[i].children;
					for(j = 0; j<children1.length;j++){
						Ids.push(children1[j].id);
					}
				}	
		   	}
			console.log(Ids);
	   }
	   $.post({
		   url: path +"/authority/RoleRel/updateRoleRel",
		   data:{
			  
			   userId:1
			   ,roleId:$("#roleId").val()
			   , menuIds:ids
		   },
		   success:function(res){
			   if(res.code == 0){
				   layer.msg("修改成功");
				   layer.closeAll();
			   }else{
				   layer.msg("修改失败");
			   }
		   }
		   
	   })
	  }
	});
	
	
  });
  </script>